package dyyx.util;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

public abstract class WebUtil {
	
	private static volatile String  webRootDir = null;
	
	
			
   
	
	public synchronized static void setWebRootDir(String dir){
		if(StringUtils.isBlank(dir)){
			throw new RuntimeException("dir is null");
		}
		if(webRootDir!=null){
			return;
		}
		
	    webRootDir = dir;
	}
	
	
	public static String getWebRootDir(){
		if(webRootDir==null){
			throw new RuntimeException("webRootDir is null");
		}
		
		return  webRootDir;
	}
	

	

	
	
	public static String get(HttpServletRequest request, String name) {
		if (request == null || name == null) {
			return null;
		}
		String str = request.getParameter(name);
		if (str != null) {
			str = str.trim();
		}
		return str;
	}

	public static int getInt(HttpServletRequest request, String name) {
		String str = get(request, name);
		return CommUtil.getInt(str);
	}

	public static long getLong(HttpServletRequest request, String name) {
		String str = get(request, name);
		return CommUtil.getLong(str);
	}
	
	public static String toJsonString(Object obj){
		if(obj==null){
			return "";
		}
		return JSON.toJSONString(obj, SerializerFeature.PrettyFormat,SerializerFeature.DisableCircularReferenceDetect);
	}

}
